package com.vf.admin.client.listener;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.tx.utils.TxIm;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;

@Component
public class RedisExpireListener extends KeyExpirationEventMessageListener {

    private static RedisExpireListener redisExpireListener;

    @Autowired
    public AppUserService userService;
    @Autowired
    public AppSetupService appSetupService;
    @Autowired
    public RedisUtil redisUtil;

    @PostConstruct
    public void init() {
        redisExpireListener = this;
        redisExpireListener.userService = this.userService;
        redisExpireListener.appSetupService = this.appSetupService;
        redisExpireListener.redisUtil = this.redisUtil;
    }

    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisExpireListener(@Qualifier("redisMessageListenerContainer") RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        int indexOf = StringUtils.indexOf(expiredKey, ":");
        String expiredKeySub = StringUtils.substring(expiredKey, 0, indexOf);

        //超时未回复心动消息，发送消息不再赠送礼物
        if (StringUtils.startsWith(RedisKeys.LOVELY_GOD, expiredKeySub)) {
            int index = StringUtils.indexOf(expiredKey, ":");
            int index2 = StringUtils.lastIndexOf(expiredKey, ":");

            Integer manUserId = Integer.valueOf(expiredKey.substring(index + 1, index2));
            Integer womanUserId = Integer.valueOf(expiredKey.substring(index2));

            AppUser manUser = redisExpireListener.userService.get(manUserId);
            AppUser womanUser = redisExpireListener.userService.get(womanUserId);
            TxIm.sendUserMsgqbtAlert(womanUser.getTengxuncode(), manUser.getTengxuncode(), "您没有及时回复对方的心动消息，礼物已被退回");
        } else if(StringUtils.startsWith(RedisKeys.FORBID_USER_LOGIN, expiredKeySub)) {
            //禁止登陆时间已过
            int index = StringUtils.indexOf(expiredKey, ":");
            Integer userId = Integer.valueOf(expiredKey.substring(index));
            redisExpireListener.userService.upstate(userId,1);
        } else if(expiredKeySub.startsWith(RedisKeys.WRITE_OFF)) {
            //账号注销
            int index = StringUtils.indexOf(expiredKey, ":");
            Integer userId = Integer.valueOf(expiredKey.substring(index));
            redisExpireListener.appSetupService.writeOff(userId);
        } else if(StringUtils.startsWith(RedisKeys.MESSAGE_CHAT_DEDUCTION, expiredKeySub)) {
            //超时未回复对方消息，给消息发起者退费
            int index = StringUtils.indexOf(expiredKey, ":");
            int index2 = StringUtils.lastIndexOf(expiredKey, ":");
            int lastIndexOf = StringUtils.lastIndexOf(expiredKey, "_");

            Integer userIdA = Integer.valueOf(expiredKey.substring(index + 1, index2));
            Integer userIdB = Integer.valueOf(expiredKey.substring(index2 + 1, lastIndexOf));
            AppUser userA = redisExpireListener.userService.get(userIdA);
            AppUser userB = redisExpireListener.userService.get(userIdB);

            //取得value
            String value = redisExpireListener.redisUtil.get("COPY_" + expiredKey).toString();
            int index3 = StringUtils.indexOf(value, "-");
            //取得钻石数量
            BigDecimal diamond = new BigDecimal(StringUtils.substring(value, 0, index3));
            redisExpireListener.userService.addDiamonds(userIdA, diamond, "对方超时未回复退还");
            TxIm.sendUserMsgqbtAlert(userB.getTengxuncode(), userA.getTengxuncode(), "您没有及时回复对方的消息，"+ diamond +"钻石已被退回。");
        }
    }


    public static void main(String[] args) {
        String message_chat_deduction = "MESSAGE_CHAT_DEDUCTION";
        String s = "MESSAGE_CHAT_DEDUCTION:%s:%s";
        boolean b = StringUtils.startsWith(s, message_chat_deduction);
        System.out.println(b);
    }
}
